package edu.sysu.cshare.model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.Session;
import org.hibernate.annotations.BatchSize;

import edu.sysu.cshare.util.HibernateSessionFactory;

@Entity
@Table(name = "T_MESSAGE")
public class Message {

	public Message(String title,  Category category,
			String content) {
		super();
		this.category = category;
		this.content = content;
		this.title = title;
		this.createDate= Calendar.getInstance();
	}
	public boolean hasAttach(){
		return (this.attachPath!=null && this.attachPath.length()!=0);
	}
	public Message(String title, User publisher, Category category,
			String content) {
		super();
		this.category = category;
		this.publisher=publisher;
		this.content = content;
		this.title = title;
		this.createDate= Calendar.getInstance();
	}
	public static enum Category {
		Class, All, Course, Other
	}

	private String attachPath;
	private Category category;
	private List<Comment> comments = new ArrayList<Comment>();
	private String content;
	private Calendar createDate;
	private long id;
	private User publisher;
	private String title;

	
	public Message() {

	}

	public String getAttachPath() {
		return attachPath;
	}

	@Enumerated(EnumType.STRING)
	public Category getCategory() {
		return category;
	}

	@OneToMany(mappedBy = "message", targetEntity=Comment.class,cascade = CascadeType.REMOVE ,fetch=FetchType.EAGER)
	public List<Comment> getComments() {
		return comments;
	}

	public String getContent() {
		return content;
	}

	public Calendar getCreateDate() {
		return createDate;
	}

	@Id
	@GeneratedValue
	public long getId() {
		return id;
	}

	@ManyToOne
	public User getPublisher() {
		return publisher;
	}

	public String getTitle() {
		return title;
	}

	public void setAttachPath(String attachPath) {
		this.attachPath = attachPath;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setCreateDate(Calendar createDate) {
		this.createDate = createDate;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void setPublisher(User publisher) {
		this.publisher = publisher;
	}

	public void setTitle(String title) {
		this.title = title;
	}
}
